﻿using System.Collections.Generic;
using System.IO;

namespace FemtoHttpd
{
    public class HeaderReader
    {
        Stream _stream;
        readonly int max;
        int count = 0;
        public HeaderReader(Stream stream, int max = 1024 * 8)
        {
            _stream = stream;
            this.max = max;
        }
        public bool ReadLine(out string line)
        {
            char[] cs = new char[max];
            int index = 0;//有效字符数
            int c = -1;
            while (count++ < max && (c = _stream.ReadByte()) > 31 && c < 127)
            {
                cs[index++] = (char)c;
            }
            if (c == 13 && _stream.ReadByte() == 10)
            {
                line = new string(cs, 0, index);
                return true;
            }
            line = null;
            return false;
        }
        public bool ReadAllLine(out string[] lines)
        {
            List<string> list = new List<string>();
            while (ReadLine(out string line))
            {
                if (line != "")
                {
                    list.Add(line);
                }
                else
                {
                    break;
                }
            }
            lines = list.ToArray();
            return list.Count > 0;
        }
    }
}
